package com.banmao.learnandorid.recycleView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;
import android.widget.Toast;

import com.banmao.learnandorid.R;
import com.banmao.learnandorid.entity.Person;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class RecycleViewActivity extends AppCompatActivity {

    private List<Person> data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycle_view);

        // 生成数据
        for (int i = 0; i < 50; i++) {
            String uuid = UUID.randomUUID().toString().replace("-", "");
            data.add(new Person("张" + i, uuid, Long.valueOf(uuid.hashCode())));
        }

        RecyclerView recyclerView = findViewById(R.id.rv);

        // 设置 RecyclerView 的布局
        // 线性布局 和listView一样
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);

        // 网格显示
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3); // 每行显示3个;
        recyclerView.setLayoutManager(gridLayoutManager);

        // 瀑布流
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);

        /**
         *  瀑布流和网格的区别：
         *      瀑布流在一行无法对其的时候自动将下一行提上来进行填充
         *      网格则会留白
         */

        MyAdapter myAdapter = new MyAdapter(data, this);
        recyclerView.setAdapter(myAdapter);

        // 设置监听
        myAdapter.setRecyclerItemClickListener(new MyAdapter.OnRecyclerItemClickListener() {
            @Override
            public void onRecyclerItemClick(int position) {
                Toast.makeText(RecycleViewActivity.this, "点击了 第" + position + "个", Toast.LENGTH_SHORT).show();
            }
        });

    }
}